﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WucCloud.Utility
{
    /// <summary>
    /// 树实体
    /// </summary>
    public class TreeNode
    {
        /// <summary>
        /// 节点
        /// </summary>
        public string id { get; set; }
        /// <summary>
        /// 父节点
        /// </summary>
        public string pid { get; set; }
        /// <summary>
        /// 节点名称
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 节点图标
        /// </summary>
        public string icon { get; set; }
        /// <summary>
        /// 节点地址
        /// </summary>
        public string href { get; set; }
        /// <summary>
        /// 子节点
        /// </summary>
        public List<TreeNode> childen { get; set; }
    }


    /// <summary>
    /// 树 拓展
    /// </summary>
    public static class TreeNodeExtensions
    {
        /// <summary>
        /// 查找所有节点 设置根节点0
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static List<TreeNode> TreeList(this List<TreeNode> data)
        {
            return TreeList(data, "0");
        }

        /// <summary>
        /// 根据父级节点Id查找所有子节点
        /// </summary>
        /// <param name="data"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private static List<TreeNode> TreeList(List<TreeNode> data, string parentId)
        {
            var ChildNodeList = data.FindAll(t => t.pid == parentId);
            if (ChildNodeList.Count > 0)
            {
                foreach (TreeNode entity in ChildNodeList)
                {
                    entity.childen = TreeList(data, entity.id);
                }
            }
            return ChildNodeList;
        }
    }
}
